package inf.coach.mobile;

import java.text.DecimalFormat;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;

import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class Station extends Activity {
    public static long userid = 0;
    public static long osmid= 0;
    public static String rate;
    public static String name;
    
    private Button station_info;
    private Button nachbar_info;
    private Button kommentar;
    private Button addkommentar;
    private Button bewertung;
    private TextView info;
    private TextView safety;
    private TextView accessibility;
    private TextView orientation;
    private assessment ass;
	
	 public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.station);
	        setTitle("Haltestelle informationen");
	        
	       	Bundle bundle = this.getIntent().getExtras();      
			userid = bundle.getLong("userid");
			osmid= bundle.getLong("osmid");
			rate= bundle.getString("rate");
			name=bundle.getString("name");
			
			info=(TextView) findViewById(R.id.textView_station_info);
			safety=(TextView) findViewById(R.id.textView_station_safety);
			accessibility=(TextView) findViewById(R.id.textView_station_accessibility);
			orientation=(TextView) findViewById(R.id.textView_station_orientation);
			
			station_info=(Button) findViewById(R.id.button_station_info);
			nachbar_info=(Button) findViewById(R.id.button_nachbar_info);
			kommentar=(Button) findViewById(R.id.button_kommentar);
			addkommentar=(Button) findViewById(R.id.button_kommentar_add);
			bewertung=(Button) findViewById(R.id.button_bewerten);
			
			if(ConnectionReceiver.isInternetConnected(getApplicationContext()))
			{										
				ass = webservice.getAssessment(osmid);
				info.setText(name);
				DecimalFormat df  = new DecimalFormat("#.0");
				System.out.println(ass.safety);
				info.setText("Haltestellenname: "+name);
				safety.setText("Sicherheit : "+df.format(ass.safety).toString()+" punkt");
				accessibility.setText("Brrierefreiheit : "+df.format(ass.accessibility).toString()+" punkt");
				orientation.setText("Orientierung : "+df.format(ass.orientation).toString()+" punkt");
				
				
			}
			else
			{
				message.show(Station.this,"Fehler", "Keine Internetverbindung! ");
			}
			
			
			station_info.setOnClickListener(new Button.OnClickListener() {

				public void onClick(View arg0) {
					
					if(ConnectionReceiver.isInternetConnected(getApplicationContext()))
					{										
						stationannotation st = webservice.getStationAnno(osmid);
					
					if (st==null)
					{
						message.show(Station.this,"Fehler", "Verbindung zur Datenbank fehlgeschlagen");
					}
					else if(st.osm_id==-1)
					{
						dialog("inf.coach.mobile.Stationanno");
					}
					else
					{
						Intent intent = new Intent();
    					Bundle bundle = new Bundle();
    					bundle.putBoolean("flag",true);
    					bundle.putLong("osmid", osmid);
    					bundle.putLong("userid", userid);
    					bundle.putDouble("lat", st.lat);
    					bundle.putDouble("lon", st.lon);
    					bundle.putString("line", st.line);
    					bundle.putBoolean("bus",st.bus);
    					bundle.putBoolean("str", st.str);
    					bundle.putBoolean("doppel", st.doppel);
       					bundle.putBoolean("shed", st.shed);
    					intent.setClass(Station.this, Stationanno.class);
    					intent.putExtras(bundle);
    					startActivity(intent);
					}
					}
					

	        			else{
	    					message.show(Station.this,"Fehler", "Keine Internetverbindung");
	    				}
	        	}
					

			});

			kommentar.setOnClickListener(new Button.OnClickListener(){
				public void onClick(View arg0){
					
					Intent intent = new Intent();
					Bundle bundle = new Bundle();
					bundle.putLong("osmid", osmid);
					bundle.putLong("userid", userid);
					intent.setClass(Station.this, CommentList.class);
					intent.putExtras(bundle);
					startActivity(intent);
				}
			});
			addkommentar.setOnClickListener(new Button.OnClickListener(){
				public void onClick(View arg0){
					
					Intent intent = new Intent();
					Bundle bundle = new Bundle();
					bundle.putLong("osmid", osmid);
					bundle.putLong("userid", userid);
					intent.setClass(Station.this, CommentType.class);
					intent.putExtras(bundle);
					startActivity(intent);
				}
				
			});
			bewertung.setOnClickListener(new Button.OnClickListener(){
				public void onClick(View arg0){
					
					Intent intent = new Intent();
					Bundle bundle = new Bundle();
					bundle.putLong("osmid", osmid);
					bundle.putLong("userid", userid);
					intent.setClass(Station.this, Rating.class);
					intent.putExtras(bundle);
					startActivity(intent);
				}
				
			});
	 }
	 protected void dialog( String classname) { 
		 
		 	final String name=classname;
	        AlertDialog.Builder builder = new Builder(Station.this); 
	        builder.setMessage("Es gibt Keine Annotation dafür, Möchten Sie jetzt erstellen ?"); 
	        builder.setTitle("Information"); 
	        builder.setPositiveButton("ok", 
	                new android.content.DialogInterface.OnClickListener() { 
	                    @Override 
	                    public void onClick(DialogInterface dialog, int which) { 
	                        dialog.dismiss(); 
	                        Intent intent = new Intent();
	    					Bundle bundle = new Bundle();
	    					bundle.putBoolean("flag", false);
	    					bundle.putLong("osmid", osmid);
	    					bundle.putLong("userid", userid);
	    					intent.setClassName(Station.this, name);
	    					intent.putExtras(bundle);
	    					startActivity(intent);
	                    }

	                }); 
	        builder.setNegativeButton("cancel", 
	                new android.content.DialogInterface.OnClickListener() { 
	                    @Override 
	                    public void onClick(DialogInterface dialog, int which) { 
	                        dialog.dismiss(); 
	                    } 
	                }); 
	        builder.create().show(); 
	    }
	 protected void dialoggoback() { 
	        AlertDialog.Builder builder = new Builder(Station.this); 
	        builder.setMessage("Möchten Sie zur letzten Seite zurück springen?"); 
	        builder.setTitle("Information"); 
	        builder.setPositiveButton("ok", 
	                new android.content.DialogInterface.OnClickListener() { 
	                    @Override 
	                    public void onClick(DialogInterface dialog, int which) { 
	                        dialog.dismiss(); 
	                        Station.this.finish(); 
	                    }

	                }); 
	        builder.setNegativeButton("abbrechen", 
	                new android.content.DialogInterface.OnClickListener() { 
	                    @Override 
	                    public void onClick(DialogInterface dialog, int which) { 
	                        dialog.dismiss(); 
	                    } 
	                }); 
	        builder.create().show(); 
	    } 
	    @Override 
	    public boolean onKeyDown(int keyCode, KeyEvent event) { 
	        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { 
	            dialoggoback(); 
	            return false; 
	        } 
	        return false; 
	    } 
}

